package candidateView;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.xml.stream.XMLStreamException;

import xml.MockVotingXMLWriter;

import electionCtrl.Admin;
import electionCtrl.Candidate;
import electionCtrl.Election;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.File;

public class CandidateListRunner
{
	public static void main (String [] args) throws FileNotFoundException, XMLStreamException, IOException, java.lang.InterruptedException
	{
		CandidateList c = new CandidateList();
		c.enable();
		c.showWindow();
		Candidate[] candidates = c.getList();
		c.disable();
		c.hideWindow();
		Admin a = c.getAdmin();
		String outputLoc = "";
		
		JFileChooser chooser = new JFileChooser();
		chooser.setDialogType(JFileChooser.SAVE_DIALOG); 
		JOptionPane.showMessageDialog(null, "Select a location to save the ballot definition.");
		int rc = chooser.showSaveDialog(null);
		if (rc == JFileChooser.APPROVE_OPTION) {
			outputLoc = chooser.getSelectedFile().getAbsolutePath()+".blt";
		}
		
		while(new File(outputLoc).exists()) {
					JOptionPane.showMessageDialog(null, "You selected a file that already exists. Please enter a new file name so as to ensure nothing is overwritten.");
					JOptionPane.showMessageDialog(null, "Select a location to save the ballot definition.");
					rc = chooser.showSaveDialog(null);
					if (rc == JFileChooser.APPROVE_OPTION) {
						outputLoc = chooser.getSelectedFile().getAbsolutePath()+".blt";
					}
				}
		
			MockVotingXMLWriter writer = new MockVotingXMLWriter(outputLoc);
			Election e = new Election(candidates,a);
			e.writeCandidatesToXML(writer);
	}
}
